import { Box, Button, Checkbox, IconButton, InputAdornment, Table, TableBody, TableContainer, TableFooter, TableHead, TableRow, TextField } from '@mui/material';
import React, { JSX } from 'react';
import { selectCommodityFetched, fetchAllCommodityEntries, selectCommodityEntries } from '../../actions/CommodityManagement';
import { InventoryEntry, Warehouse, deleteInventory, fetchAllInventoryEntries, fetchAvailableWarehouses, selectInventoryAvailableWarehouses, selectInventoryAvailableWarehousesFetched, selectInventoryEntries } from '../../actions/InventoryManagement';
import { fetchAllWarehouse, selectWarehouseEntries, selectWarehouseFetched } from '../../actions/WarehouseManagement';
import { useAppDispatch, useAppSelector } from '../../actions/hook';
import { NulHeaderTableCell, NulTableCell } from '../../components/NulHeaderTable';
import { removeElementFromArray } from '../../util/Arrays';
import ClearIcon from '@mui/icons-material/Clear';
import EditIcon from '@mui/icons-material/Edit';
import DeleteIcon from '@mui/icons-material/Delete';
import { ReactComponent as SearchIcon } from '../../assets/search.svg';
import { ReactComponent as CreateIcon } from '../../assets/create.svg';
import AirlineStopsIcon from '@mui/icons-material/AirlineStops';
import AutorenewIcon from '@mui/icons-material/Autorenew';
import SwitchInventoryDialog from '../InventoryDialog/SwitchInventoryDialog';
import TransferInventoryDialog from '../InventoryDialog/TransferInventoryDialog';
import { CreateInventory } from '../InventoryDialog/CreateInventoryDialog';
import { fetchAllSupplierEntries, selectSupplierEntries, selectSupplierFetched } from '../../actions/SupplierManagement';
import { ModifyInventory } from '../InventoryDialog/ModifyInventoryDialog';

interface InventoryManagementProps {

}

function InventoryManagement(props: InventoryManagementProps): JSX.Element {

  const [filterValue, setFilterValue] = React.useState<string>('');
  const [openInventoryDialog, setOpenInventoryDialog] = React.useState<boolean>(false);
  const [openModifyDialog, setOpenModifyDialog] = React.useState<boolean>(false);
  const [openSwitchDialog, setOpenSwitchDialog] = React.useState<boolean>(false);
  const [openTransferDialog, setOpenTransferDialog] = React.useState<boolean>(false);
  const [currentWarehouse, setCurrentWarehouse] = React.useState<Warehouse>({
    id: 0, name: ''
  });

  const [filteredWarehouseEntries, setFilteredWarehouseEntries] = React.useState<InventoryEntry[]>([]);
  const [checkedIdList, setCheckedIdList] = React.useState<number[]>([]);
  const [modifyEntry, setModifyEntry] = React.useState<InventoryEntry>({
    id: 0, name: '', quantity: 0, supplierName: '', costPrice: 0, retailPrice: 0, wholesalePrice: 0
  });

  const dispatch = useAppDispatch();
  
  const warehouseEntriesFetched = useAppSelector(selectWarehouseFetched);
  const warehouseEntries = useAppSelector(selectWarehouseEntries);
  const inventoryEntries = useAppSelector(selectInventoryEntries);
  const supplierEntriesFetched = useAppSelector(selectSupplierFetched);
  const supplierEntries = useAppSelector(selectSupplierEntries);
  const commodityEntries = useAppSelector(selectCommodityEntries);
  const commodityEntriesFetched = useAppSelector(selectCommodityFetched);

  const fetchedAvailabelWarehouses = useAppSelector(selectInventoryAvailableWarehousesFetched);
  const availableWarehouses = useAppSelector(selectInventoryAvailableWarehouses);

  const getSelectedEntries = () => {
    const result: InventoryEntry[] = [];
    for (let i = 0; i < checkedIdList.length; ++i){
      const find = inventoryEntries.find((value) => value.id === checkedIdList[i]);
      if (find !== undefined) {
        result.push(find);
      }
    }
    return result;
  };

  React.useEffect(() => {
    if (!fetchedAvailabelWarehouses) {
      dispatch(fetchAvailableWarehouses());
    }
    if (!supplierEntriesFetched) {
      dispatch(fetchAllSupplierEntries());
    }
    if (!commodityEntriesFetched) {
      dispatch(fetchAllCommodityEntries());
    }
    if (!warehouseEntriesFetched) {
      dispatch(fetchAllWarehouse());
    }
  });

  React.useEffect(() => {
    if (fetchedAvailabelWarehouses) {
      if (availableWarehouses.length > 0) {
        setCurrentWarehouse(availableWarehouses[0]);
        dispatch(fetchAllInventoryEntries({ warehouseId: availableWarehouses[0].id }));
      }
    }
  }, [fetchedAvailabelWarehouses, availableWarehouses, dispatch]);

  React.useEffect(() => {
    if (inventoryEntries !== undefined) {
      setFilteredWarehouseEntries(inventoryEntries.filter(val => val.name.indexOf(filterValue) !== -1));
      setCheckedIdList([]);
    }
  }, [inventoryEntries, filterValue]);

  const handleModify = (row: InventoryEntry) => {
    setModifyEntry(row);
    setOpenModifyDialog(true);
  };

  const handleDelete = (row: InventoryEntry) => {
    dispatch(deleteInventory({ id: [ row.id ], warehouseId: currentWarehouse.id }));
  };

  const handleBatchDelete = () => {
    dispatch(deleteInventory({ id: checkedIdList, warehouseId: currentWarehouse.id }));
  }

  const handleSwitchResult = (id: number) => {
    const index: number = availableWarehouses.findIndex(value => value.id === id);
    if (index !== -1) {
      setCurrentWarehouse(availableWarehouses[index]);
      setCheckedIdList([]);
      dispatch(fetchAllInventoryEntries({ warehouseId: id }));
    }
  };

  const headerChecked: boolean = checkedIdList.length !== 0 && checkedIdList.length === filteredWarehouseEntries.length;
  const headerIndeterminate: boolean = checkedIdList.length !== 0 && checkedIdList.length < filteredWarehouseEntries.length;

  return (
    <>
      <Box sx={{ padding: '30px 30px 30px 30px', flex: 'none', flexGrow: 1 }}>
        <Box sx={{
          fontFamily: 'Open Sans',
          fontStyle: 'normal',
          fontWeight: 600,
          fontSize: '22px',
          lineHeight: '125%',
          display: 'flex',
          flexDirection: 'row'
        }}>
          <Box>
            Inventory Management
          </Box>
          <Button
            startIcon={<AutorenewIcon />} sx={{ marginLeft: '10px', textTransform: 'none' }}
            onClick={() => { setOpenSwitchDialog(true); }}
          >
            Switch
          </Button>
        </Box>
        <Box sx={{
          marginTop: '20px', width: '100%', height: '40px', display: 'flex',
          flexDirection: 'row', justifyContent: 'space-between'
        }}>
          <Box sx={{ width: '456px' }}>
            <TextField
              size='small'
              fullWidth={true}
              InputProps={{
                startAdornment: (
                  <InputAdornment position='start' sx={{}}>
                    <SearchIcon />
                  </InputAdornment>
                ),
                endAdornment: (
                  <IconButton edge="end" onClick={() => { setFilterValue(''); }}>
                    <ClearIcon fontSize='small' />
                  </IconButton>
                )
              }}
              placeholder='Search inventory'
              value={filterValue}
              onChange={(action) => { setFilterValue(action.target.value); }}
              sx={{
                paddingLeft: '0px'
              }}
            />
          </Box>
          <Box sx={{
            flex: 'none',
            order: 1,
            flexGrow: 0,
          }}>
            <Button
              variant="outlined"
              disableElevation
              startIcon={<AirlineStopsIcon />}
              onClick={() => {
                setOpenTransferDialog(true);
              }}
              sx={{
                height: '40px',
                textTransform: 'none',
                marginRight: '10px',
                minWidth: '110px'
              }}
            >
              Transfer
            </Button>
            <Button
              variant="contained"
              disableElevation
              startIcon={<DeleteIcon />}
              color='error'
              onClick={handleBatchDelete}
              sx={{
                height: '40px',
                textTransform: 'none',
                marginRight: '10px',
                minWidth: '95px'
              }}
            >
              Delete
            </Button>
            <Button
              variant="contained"
              disableElevation
              startIcon={<CreateIcon />}
              onClick={() => {
                setOpenInventoryDialog(true);
              }}
              sx={{
                height: '40px',
                textTransform: 'none',
                minWidth: '95px'
              }}
            >
              Stock
            </Button>
          </Box>
        </Box>
        <Box sx={{
          marginTop: '20px',
          width: '100%'
        }}>
          <TableContainer
            sx={{
              width: '100%'
            }}
          >
            <Table
              stickyHeader
              sx={{
                border: '1px solid #DFE4E8',
                borderRadius: '8px',
                fontFamily: 'Open Sans',
                fontStyle: 'normal',
              }}
            >
              <TableHead sx={{ border: '8px 8px 0px 0px' }}>
                <TableRow sx={{
                  height: '42px',
                  padding: 0,
                  width: '100%',
                  display: 'flex',
                  alignItems: 'center',
                }}>
                  <Box sx={{ height: '100%' }}>
                    <NulHeaderTableCell
                      sx={{
                        width: '62px',
                        borderTopLeftRadius: '8px',
                        justifyContent: 'center',
                        flexGrow: 0
                      }}
                    >
                      <Checkbox
                        sx={{
                          width: '42px',
                          display: 'flex',
                          justifyContent: 'center',
                        }}
                        onClick={() => {
                          if (headerChecked) {
                            setCheckedIdList([]);
                          }
                          else {
                            setCheckedIdList(filteredWarehouseEntries.map(entry => entry.id));
                          }
                        }}
                        checked={headerChecked}
                        indeterminate={headerIndeterminate}
                      />
                    </NulHeaderTableCell>
                  </Box>
                  <Box sx={{ display: 'flex', flexDirection: 'row', flexGrow: 1, position: 'relative', height: '100%' }}>
                    <NulHeaderTableCell sx={{ width: '5%' }}>
                      ID
                    </NulHeaderTableCell>
                    <NulHeaderTableCell sx={{ width: '20%' }}>
                      Name
                    </NulHeaderTableCell>
                    <NulHeaderTableCell sx={{ width: '15%' }}>
                      Quantity
                    </NulHeaderTableCell>
                    <NulHeaderTableCell sx={{ width: '20%' }}>
                      Supplier
                    </NulHeaderTableCell>
                    <NulHeaderTableCell sx={{ width: '15%' }}>
                      Cost Price
                    </NulHeaderTableCell>
                    <NulHeaderTableCell sx={{ width: '25%', borderTopRightRadius: '8px' }}>
                      Operations
                    </NulHeaderTableCell>
                  </Box>
                </TableRow>
              </TableHead>
              <TableBody
                sx={{
                  minHeight: '400px', maxHeight: '400px', display: 'block', overflowY: 'auto'
                }}
              >
                {filteredWarehouseEntries.map(row => {
                  const checkedIndex: number = checkedIdList.indexOf(row.id);
                  return (
                    <TableRow
                      key={row.id}
                      sx={{
                        width: '100%',
                        display: 'flex',
                        alignItems: 'center',
                        height: '42px'
                      }}
                    >
                      <Box sx={{ height: '100%' }}>
                        <NulTableCell sx={{ width: '62px', justifyContent: 'center', flexGrow: 0 }}>
                          <Checkbox
                            sx={{
                              width: '42px',
                              display: 'flex',
                              justifyContent: 'center',
                            }}
                            checked={checkedIndex !== -1}
                            onChange={(event, checked) => {
                              if (checked) {
                                if (checkedIndex === -1) {
                                  setCheckedIdList([...checkedIdList, row.id]);
                                }
                              }
                              else {
                                if (checkedIndex !== -1) {
                                  setCheckedIdList(removeElementFromArray(checkedIdList, checkedIndex));
                                }
                              }
                            }}
                          />
                        </NulTableCell>
                      </Box>
                      <Box sx={{ display: 'flex', flexDirection: 'row', flexGrow: 1, position: 'relative', height: '100%' }}>
                        <NulTableCell sx={{ width: '5%' }}>
                          {row.id}
                        </NulTableCell>
                        <NulTableCell sx={{ width: '20%' }}>
                          {row.name}
                        </NulTableCell>
                        <NulTableCell sx={{ width: '15%' }}>
                          {row.quantity}
                        </NulTableCell>
                        <NulTableCell sx={{ width: '20%' }}>
                          {row.supplierName}
                        </NulTableCell>
                        <NulTableCell sx={{ width: '15%' }}>
                          {row.costPrice}
                        </NulTableCell>
                        <NulTableCell sx={{ width: '25%', display: 'flex' }}>
                          <IconButton aria-label='edit' onClick={() => { handleModify(row); }}>
                            <EditIcon />
                          </IconButton>
                          <IconButton aria-label='delete' onClick={() => { handleDelete(row); }}>
                            <DeleteIcon />
                          </IconButton>
                        </NulTableCell>
                      </Box>
                    </TableRow>
                  );
                })}
              </TableBody>
              <TableFooter
                sx={{
                  minHeight: '34px',
                  maxHeight: '44px',
                  display: 'block',
                  backgroundColor: '#F1F3F5',
                  borderRadius: '0px 0px 8px 8px',
                }}
              ></TableFooter>
            </Table>
          </TableContainer>
        </Box>
      </Box>
      <SwitchInventoryDialog
        open={openSwitchDialog}
        onClose={() => { setOpenSwitchDialog(false); }}
        onResult={handleSwitchResult}
        warehouseEntries={availableWarehouses}
      />
      <TransferInventoryDialog
        open={openTransferDialog}
        onClose={() => { setOpenTransferDialog(false); }}
        selectedInventories={getSelectedEntries()}
        warehouseEntries={warehouseEntries}
        warehouseId={currentWarehouse.id}
      />
      <CreateInventory
        open={openInventoryDialog}
        onClose={() => { setOpenInventoryDialog(false); }}
        commodityEntries={commodityEntries}
        supplierEntries={supplierEntries}
        warehouseId={currentWarehouse.id}
      />
      <ModifyInventory
        open={openModifyDialog}
        onClose={() => { setOpenModifyDialog(false); }}
        commodityEntries={commodityEntries}
        supplierEntries={supplierEntries}
        warehouseId={currentWarehouse.id}
        modifyEntry={modifyEntry}
      />
    </>
  );
}

export default InventoryManagement;